package com.meilai.project.controller.web.finance.contractManage;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.service.finance.contractManage.FinancialSummaryService;
import com.meilai.project.vo.finance.contractManage.BillSaleGroupVO;
import com.meilai.project.vo.finance.contractManage.ContractBillSaleGroupVO;
import com.meilai.project.vo.finance.contractManage.ContractCustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 财务合同汇总 前端控制器
 * </p>
 *
 * @author xhy
 * @since 2022-12-19
 */
@RestController
@Api(value = "财务合同汇总", tags = {"财务合同汇总"})
@RequestMapping("/api/web/financial_summary")
public class FinancialSummaryController {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FinancialSummaryService financialSummaryService;

    @GetMapping("/contract_list")
    @ApiOperation("合同列表")
    public ApiResult<Page<ContractCustomerVO>> contractList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                            @RequestParam(value = "page_size", defaultValue = "10") Integer page_size,
                                                            @RequestParam(value = "customer_id", required = false) Long customer_id,
                                                            @RequestParam(value = "contract_begin_date", required = false)
                                                            @JsonFormat(pattern = "yyyy-MM-dd")
                                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_begin_date,
                                                            @RequestParam(value = "contract_end_date", required = false)
                                                            @JsonFormat(pattern = "yyyy-MM-dd")
                                                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_end_date,
                                                            @RequestParam(value = "contract_num", required = false) String contract_num) {
        Map<String, Object> req = new HashMap<>();
        req.put("page", page);
        req.put("page_size", page_size);
        req.put("customer_id", customer_id);
        req.put("contract_begin_date", contract_begin_date);
        req.put("contract_end_date", contract_end_date);
        req.put("contract_num", contract_num);
        ResponseEntity<ApiResult> forEntity = restTemplate.getForEntity("https://mock.apifox.cn/m1/1754103-0-default/api/web/financial_summary/contract_list", ApiResult.class, req);
        ApiResult body = forEntity.getBody();
        return body;
    }

    @GetMapping("/group_list")
    @ApiOperation("分组id")
    public ApiResult<Page<BillSaleGroupVO>> group_list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "page_size", defaultValue = "10") Integer page_size,
                                                       @RequestParam(value = "begin_date", required = false)
                                                       @JsonFormat(pattern = "yyyy-MM-dd")
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin_date,
                                                       @RequestParam(value = "end_date", required = false)
                                                       @JsonFormat(pattern = "yyyy-MM-dd")
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end_date,
                                                       @RequestParam(value = "execute_amount", required = false) BigDecimal execute_amount) {
        Map<String, Object> req = new HashMap<>();
        req.put("page", page);
        req.put("page_size", page_size);
        req.put("begin_date", begin_date);
        req.put("end_date", end_date);
        req.put("execute_amount", execute_amount);
        ResponseEntity<String> forEntity = restTemplate.getForEntity("https://mock.apifox.cn/m1/1754103-0-default/api/web/financial_summary/group_list", String.class, req);
        List<BillSaleGroupVO> result = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
        Page data = jsonObject.getObject("data", Page.class);
        List records = data.getRecords();
        for (Object record : records) {
            JSONObject obj = (JSONObject) record;
            BillSaleGroupVO vo = obj.toJavaObject(BillSaleGroupVO.class);
            result.add(vo);
        }
        data.setRecords(result.stream().sorted(Comparator.comparing(BillSaleGroupVO::getId)).collect(Collectors.toList()));
        return ApiResult.ok(data);
    }

    @GetMapping("/list")
    @ApiOperation("分页列表")
    public ApiResult<Page<ContractBillSaleGroupVO>> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(value = "page_size", defaultValue = "10") Integer page_size,
                                                         @RequestParam(value = "customer_id", required = false) Long customer_id,
                                                         @RequestParam(value = "contract_num", required = false) String contract_num,
                                                         @RequestParam(value = "contract_begin_date_start", required = false)
                                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_begin_date_start,
                                                         @RequestParam(value = "contract_begin_date_end", required = false)
                                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_begin_date_end,
                                                         @RequestParam(value = "contract_end_date_start", required = false)
                                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_end_date_start,
                                                         @RequestParam(value = "contract_end_date_end", required = false)
                                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_end_date_end) {
        return ApiResult.ok(financialSummaryService.list(page, page_size, customer_id, contract_num, contract_begin_date_start, contract_begin_date_end, contract_end_date_start, contract_end_date_end));
    }

    @PostMapping("/receiveContract")
    @ApiOperation("收到合同")
    public ApiResult<Boolean> receiveContract(@RequestParam("id") Long id) {
        return ApiResult.ok(financialSummaryService.receiveContract(id));
    }

    @PostMapping("/fill")
    @ApiOperation("填入入档编号")
    public ApiResult<Boolean> fill(@RequestParam("id") Long id, @RequestParam("no") String no) {
        return ApiResult.ok(financialSummaryService.fill(id, no));
    }

    @PostMapping("/remind")
    @ApiOperation("填入入档编号")
    public ApiResult<Boolean> remind(@RequestParam("id") Long id, @RequestParam("group_id") Long group_id) {
        return ApiResult.ok(financialSummaryService.remind(id, group_id));
    }

    @PostMapping("/updateDate")
    @ApiOperation("修改收款时间")
    public ApiResult<Boolean> updateDate(@RequestParam("date") @JsonFormat(pattern = "yyyy-MM-dd")
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @RequestParam("group_id") Long group_id) {
        return ApiResult.ok(financialSummaryService.updateDate(date, group_id));
    }

    @GetMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response,
                       @RequestParam(value = "customer_id", required = false) Long customer_id,
                       @RequestParam(value = "contract_num", required = false) String contract_num,
                       @RequestParam(value = "contract_begin_date_start", required = false)
                       @JsonFormat(pattern = "yyyy-MM-dd")
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_begin_date_start,
                       @RequestParam(value = "contract_begin_date_end", required = false)
                       @JsonFormat(pattern = "yyyy-MM-dd")
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_begin_date_end,
                       @RequestParam(value = "contract_end_date_start", required = false)
                       @JsonFormat(pattern = "yyyy-MM-dd")
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_end_date_start,
                       @RequestParam(value = "contract_end_date_end", required = false)
                       @JsonFormat(pattern = "yyyy-MM-dd")
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate contract_end_date_end) {
        try {
            financialSummaryService.export(response, customer_id, contract_num, contract_begin_date_start, contract_begin_date_end, contract_end_date_start, contract_end_date_end);
        } catch (Exception e) {
            System.out.println("导出失败");
        }
    }
}
